<?php
namespace app\share\controller;

use app\common\controller\Base;

class Upload extends Base
{
    private $result = [
        'msg'  => '上传成功',
        'code' => 200,
        'data' => [],
    ];
    
    protected function initialize()
    {
        parent::initialize(); 
        
        $this->auth['check'] = true;
        
        $this->auth['cancel_limit'] = 'share/upload/index';
        
        $this->authCheck();
    }
    
    public function index()
    {
        return json($this->result);
    }
    
    public function image()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = $this->request->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->rule('md5')->validate(['size'=>config('upload.size'),'ext'=>config('upload.ext')])->move(config('upload.path'));
        if($info){
            // 输出 jpg
            $this->result['data']['ext'] = $info->getExtension();
            // 输出 md5值
            $this->result['data']['md5'] = $info->hash('md5');
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            $this->result['data']['fileName'] = $info->getFilename();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $this->result['data']['saveName'] = str_replace('\\','/',$info->getSaveName());
            // 输出 http://xxxxx/uploads/savename.jpg
            $this->result['data']['cdnUrl'] = config('upload.cdn').$this->result['data']['saveName'];
        }else{
            $this->result['msg']  = $file->getError();
            $this->result['code'] = 401;
        }
        //返回信息
        return json($this->result);
    }
}
